#include "MapWidget.h"
#include <QGraphicsScene>
#include <QMessageBox>
#include <QDebug>

MapWidget::MapWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MapWidget) {
    ui->setupUi(this);  // 通过 ui 指针设置 UI 界面

    // 加载地图
    QPixmap mapPixmap("C:/Users/LEO/Desktop/map.png");  // 替换为你的地图路径
    if (mapPixmap.isNull()) {
        qWarning("Failed to load map image!");
    }

    // 将地图加载到 QGraphicsPixmapItem 中，并添加到 QGraphicsView 中
    mapItem = new QGraphicsPixmapItem(mapPixmap);
    QGraphicsScene *scene = new QGraphicsScene(this);
    scene->addItem(mapItem);
    ui->graphicsView->setScene(scene);  // 通过 UI 中的 graphicsView 设置场景
    ui->graphicsView->setDragMode(QGraphicsView::ScrollHandDrag);  // 允许拖拽
    ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);  // 缩放时以鼠标为中心

    // 设置输入框的占位符
    ui->deEdit->setPlaceholderText("始发城市");
    ui->arrEdit->setPlaceholderText("目的地");

    // 连接输入框的信号槽
    connect(ui->deEdit, &QLineEdit::textChanged, this, &MapWidget::onDepartureCityChanged);
    connect(ui->arrEdit, &QLineEdit::textChanged, this, &MapWidget::onArrivalCityChanged);
}

void MapWidget::onDepartureCityChanged(const QString &text) {
    qDebug() << "Departure City changed to:" << text;
}

void MapWidget::onArrivalCityChanged(const QString &text) {
    qDebug() << "Arrival City changed to:" << text;
}
